package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.BrandService;
import com.tanhua.model.domain.PmsBrand;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;


    @GetMapping("/list")
    public R brand(String keyword, @RequestParam(defaultValue = "1") Integer pageNum,
                   @RequestParam(defaultValue = "5") Integer  pageSize){

        PageVo pageVo=brandService.list(keyword,pageNum,pageSize);

        return R.success(pageVo);
    }

    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @return factoryStatus
     */
    @PostMapping("/update/factoryStatus")
    public R updateFactoryStatus(Integer[] ids,Integer factoryStatus){

        if (ids == null  || factoryStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        brandService.updateFactoryStatus(ids,factoryStatus);
        return R.success("");
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     * @return
     */
    @PostMapping("/update/showStatus")
    public R updateShowStatus(Integer[] ids,Integer showStatus){

        if (ids == null  || showStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        brandService.updateShowStatus(ids,showStatus);
        return R.success("");
    }


    /**
     * 根据编号查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R  findBrandById(@PathVariable("id") Integer id){
        PmsBrand pmsBrand =brandService.findBrandById(id);
        return R.success(pmsBrand);
    }

    /**
     * 更新品牌
     * @param pmsBrand
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@RequestBody PmsBrand pmsBrand,@PathVariable("id")Long id){
        //这些属性不能为null
        if ((id ==null ||pmsBrand ==null) || (pmsBrand.getLogo() ==null || pmsBrand.getName() ==null)){
            throw new BusinessException(ErrorResult.parameterError());
        }
        pmsBrand.setId(id);
        brandService.updateById(pmsBrand);
        return R.success("");
    }

    /**
     * 根据id删除品牌
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable("id") Long id){
        if (id ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        brandService.deleteById(id);
        return R.success("");
    }


    /**
     * 添加品牌
     * @param pmsBrand
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody PmsBrand pmsBrand){
        if (pmsBrand == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        brandService.create(pmsBrand);
        return R.success("");
    }





}
